package com.aorise.controller.app;

import com.aorise.controller.BaseController;
import com.aorise.exception.DataValidationException;
import com.aorise.model.request.UpdatePwdReq;
import com.aorise.service.general.GeneralService;
import com.aorise.utils.DataValidation;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.json.JsonResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @Auther: zhouhao
 * @Date: 2019/6/17
 * @Description:
 */
@Slf4j
@Api(description = "APP个人中心接口")
@RestController
@Validated
@RequestMapping("app/userInfo")
public class AppUserInfoController extends BaseController {

    @Autowired
    GeneralService generalService;

    /**
     * 密码修改
     * HTTP 方式：POST
     * API 路径：/app/userInfo/
     * 方法名：changePwd
     * 方法返回类型：String
     */
    @ApiOperation(value = "密码修改", httpMethod = "POST", response = String.class, notes = "密码修改")
    @RequestMapping(value = "/changePwd", method = RequestMethod.POST,produces="application/json")
    public String changePwd(@Validated UpdatePwdReq pwd, BindingResult result) {
        try {
            String x = getString(pwd, result);
            if (x != null){
                return x;
            }
        }catch (DataValidationException e) {
            log.error(
                    "controller:AppUserInfoController. function:changePwd...msg: changePwd DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:AppUserInfoController. function:changePwd...msg: changePwd DataAccessException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:AppUserInfoController. function:changePwd. msg:changePwd Exception");
            log.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofFailure();
    }

    /**
     * 运维统计
     * HTTP 方式：GET
     * API 路径：/api/general/operationSta
     * 方法名：getOperationSta
     * 方法返回类型：String
     */
    @ApiOperation(value = "个人运维统计", httpMethod = "GET", response = String.class, notes = "运维统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userID", value = "个人中心运维统计",paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/Appoperations",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAppoperations(String userID) {
        Map map = null;
//        DataValidation dataValidation = new DataValidation();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            map = generalService.getAppoperations(userID);
        }catch (DataValidationException e) {
            log.error(
                    "controller:AppUserInfoController. function:getAppoperations...msg: getAppoperations DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:AppUserInfoController. function:getAppoperations...msg: getAppoperations DataAccessException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(
                    "controller:AppUserInfoController. function:getAppoperations. msg:getAppoperations Exception");
            log.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofSuccess(map);
    }

}
